package com.weiiew.recipeservice.controller;

import com.example.feignapi.client.VersionClient;
import com.weiiew.common.entity.dto.Recipe;
import com.weiiew.common.entity.qo.RecipeQo;
import com.weiiew.common.GatawayAccess.GatewayAccess;
import com.weiiew.recipeservice.service.RecipeServiceImpl;
import com.weiiew.common.utils.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/2/7
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/recipe")
public class RecipeController {
    private final RecipeServiceImpl recipeService;
    private final VersionClient versionClient;

    /**
     * 创建配方
     *
     * @param recipe 配方信息
     * @return 创建或修改结果
     */
    @PostMapping("/create")
    @GatewayAccess(GatewayAccess.GatewayType.MANAGER)
    public ApiResponse createRecipe(@RequestBody Recipe recipe, @RequestHeader("my-version") Integer version) throws Exception {
        recipe.setVersion(version);
        this.recipeService.createRecipe(recipe, recipe.getId(), version);
        return ApiResponse.ok("保存成功").append("id", recipe.getId());
    }


    /**
     * 修改配方
     *
     * @param recipe 配方信息
     * @return 创建或修改结果
     */
    @PostMapping("/modify")
    @GatewayAccess(GatewayAccess.GatewayType.MANAGER)
    public ApiResponse modifyRecipe(@RequestBody Recipe recipe, @RequestHeader("my-version") Integer version) throws Exception {
        recipe.setVersion(version);
        this.recipeService.modifyRecipe(recipe, recipe.getId(), version);
        return ApiResponse.ok("保存成功");
    }

    /**
     * 删除配方
     *
     * @param id 配方id信息
     * @return 删除结果
     */
    @PostMapping("/remove/{id}")
    @GatewayAccess(GatewayAccess.GatewayType.MANAGER)
    public ApiResponse removeRecipe(@RequestHeader("my-version") Integer version, @PathVariable Integer id) {
        System.out.println("id = " + id);
        boolean flag = this.recipeService.removeRecipe(id, version);
        return ApiResponse.response(flag, "配方删除");
    }
    @GetMapping("/get/{id}")
    @GatewayAccess(GatewayAccess.GatewayType.CLIENT)
    public ApiResponse getRecipeVO(@PathVariable Integer id){
        return ApiResponse.ok("获取成功", "recipe", recipeService.getRecipeVOById(id));
    }

    /**
     * 直接查询所有配方
     *
     * @param version 版本号
     * @return 所有配方
     */
    @PostMapping("/list")
    @GatewayAccess({GatewayAccess.GatewayType.MANAGER, GatewayAccess.GatewayType.CLIENT})
    public ApiResponse getRecipeList(RecipeQo qo, @RequestHeader("my-version") Integer version) throws Exception {
        if (version == null) throw new Exception("该版本不存在");
        qo.setVersion(version);
        return ApiResponse.ok("获取成功")
                .append("list", recipeService.getRecipeList(qo))
                .append("length", recipeService.getRecipeLength(qo));
    }

    /**
     * 获取配方页面初始化参数
     *
     * @param version 版本号
     * @return 配方页面初始化参数
     */
    @GetMapping("/init")
    @GatewayAccess({GatewayAccess.GatewayType.MANAGER, GatewayAccess.GatewayType.CLIENT})
    public ApiResponse getRecipeInit(@RequestHeader("my-version") Integer version) {
        if (version == null) return ApiResponse.failure("版本号缺失");
        return ApiResponse.ok("初始化参数获取成功")
                .append("ingredients", recipeService.getItemListSortByIngredient(version))
                .append("products", recipeService.getItemListSortByProduct(version))
                .append("buildings", recipeService.getBuildListSortByProduct(version))
                .append("versions", versionClient.getVersionList());
    }

    /**
     * 根据版本id和物品id返回该物品的合成配方列表
     *
     * @param version 版本号
     * @param item    物品id
     * @return 合成配方列表
     */
    @RequestMapping("/cal/list")
    @GatewayAccess(GatewayAccess.GatewayType.CLIENT)
    public ApiResponse getRecipeListByItem(@RequestHeader("my-version") Integer version, Integer item) {
        if (version == null) return ApiResponse.failure("版本号为空");
        if (item == null) return ApiResponse.failure("物品id为空");
        return ApiResponse.ok("获取成功", "list", recipeService.getCalRecipeByItem(version, item));
    }
}
